import { getCityList } from '@/api/auth'

// 获取省市区列表
export function getAddressList() {
  // 获取省市区列表初始化
  getCityList().then(response => {
    address.provinceList = response.data.data
  })
}

// 获取省市区列表
export function getDialogAddressList() {
  // 获取省市区列表初始化
  getCityList().then(response => {
    dialogAddress.provinceList = response.data.data
  })
}
// 根据code和name查询城市信息
export function getIndexBycode(shengId, shiId, quId) {
  address.shengId = shengId
  address.shiId = shiId
  address.quId = quId

  for (let index = 0; index < address.provinceList.length; index++) {
    if (address.provinceList[index].province.code === address.shengId) {
      address.provinceId = index
      address.cityList = address.provinceList[index].cityRegion
      console.log(address.cityList)
      console.log(address.shiId)
      for (let i = 0; i < address.cityList.length; i++) {
        if (address.cityList[i].city.code === address.shiId) {
          address.cityId = i
          address.villageList = address.cityList[i].village
          console.log(address.cityList)
          for (let k = 0; k < address.villageList.length; k++) {
            if (address.villageList[k].code === address.quId) {
              address.villageId = k
              console.log(address.villageList)
            }
          }
        }
      }
    }
  }

  /* for (const [item, index] of address.provinceList.entries()) {
      if (item.province.code === address.shengId) {
        this.formData.shengId = index
        address.cityList = item.cityRegion
        console.log(this.formData.shengId)
        console.log(address.provinceList)
        for (const [m, i] of address.cityList.entries()) {
          if (m.city.code === address.shiId) {
            this.formData.shiId = i
            address.villageList = m.village
            console.log(address.cityList)
            for (const [j, k] of address.villageList.entries()) {
              if (j.code === address.quId) {
                this.formData.quId = k
                console.log(address.villageList)
              }
            }
          }
        }
      }
    }*/
}

// 根据code和name查询城市信息
export function getDialogIndexBycode(shengId, shiId, quId) {
  dialogAddress.shengId = shengId
  dialogAddress.shiId = shiId
  dialogAddress.quId = quId

  for (let index = 0; index < dialogAddress.provinceList.length; index++) {
    if (dialogAddress.provinceList[index].province.code === dialogAddress.shengId) {
      dialogAddress.provinceId = index
      dialogAddress.cityList = dialogAddress.provinceList[index].cityRegion
      console.log(dialogAddress.cityList)
      console.log(dialogAddress.shiId)
      for (let i = 0; i < dialogAddress.cityList.length; i++) {
        if (dialogAddress.cityList[i].city.code === dialogAddress.shiId) {
          dialogAddress.cityId = i
          dialogAddress.villageList = dialogAddress.cityList[i].village
          console.log(dialogAddress.cityList)
          for (let k = 0; k < dialogAddress.villageList.length; k++) {
            if (dialogAddress.villageList[k].code === dialogAddress.quId) {
              dialogAddress.villageId = k
              console.log(dialogAddress.villageList)
            }
          }
        }
      }
    }
  }
}

// 获取市列表
export function getCityListByid(item) {
  if (this.formData.address.shengId) {
    address.cityList = address.provinceList[item].cityRegion
    address.shengId = address.provinceList[item].province.code
    address.sheng = address.provinceList[item].province.name
    address.villageList = null
    this.formData.address.shiId = null
    this.formData.address.quId = null
  } else {
    address.cityList = null
    address.villageList = null
    this.formData.address.shiId = null
    this.formData.address.quId = null
    /* this.formData.shopAddress.shiId = null
    this.formData.shopAddress.quId = null*/
  }
}

// 获取市列表
export function getDialogCityListByid(item) {
  if (this.form.address.shengId) {
    dialogAddress.cityList = dialogAddress.provinceList[item].cityRegion
    dialogAddress.shengId = dialogAddress.provinceList[item].province.code
    dialogAddress.sheng = dialogAddress.provinceList[item].province.name
    dialogAddress.villageList = null
    this.form.address.shiId = null
    this.form.address.quId = null
  } else {
    dialogAddress.cityList = null
    dialogAddress.villageList = null
    this.form.address.shiId = null
    this.form.address.quId = null
    /* this.formData.shopAddress.shiId = null
    this.formData.shopAddress.quId = null*/
  }
  return dialogAddress
}

// 获取市id和市名称
export function getCityInfoByid(item) {
  if (this.formData.address.shiId) {
    address.shiId = address.cityList[item].city.code
    address.shi = address.cityList[item].city.name
  }
}

// 获取市id和市名称
export function getDialogCityInfoByid(item) {
  if (this.form.address.shiId !== null) {
    dialogAddress.shiId = dialogAddress.cityList[item].city.code
    dialogAddress.shi = dialogAddress.cityList[item].city.name
  }
}

// 获取区列表
export function getVillageListByid(item) {
  if (this.formData.address.shiId) {
    address.villageList = address.cityList[item].village
    address.shiId = address.cityList[item].city.code
    address.shi = address.cityList[item].city.name
    this.formData.address.quId = null
  } else {
    address.villageList = null
    this.formData.address.quId = null
  }
}

// 获取区列表
export function getDialogVillageListByid(item) {
  if (this.form.address.shiId !== null) {
    dialogAddress.villageList = dialogAddress.cityList[item].village
    dialogAddress.shiId = dialogAddress.cityList[item].city.code
    dialogAddress.shi = dialogAddress.cityList[item].city.name
    this.form.address.quId = null
  } else {
    dialogAddress.villageList = null
    this.form.address.quId = null
  }
}

// 获取区信息
export function getVillage(item) {
  if (this.formData.address.quId !== null) {
    address.quId = address.villageList[item].code
    address.qu = address.villageList[item].name
  }
}

// 获取区信息
export function getDialogVillage(item) {
  if (this.form.address.quId !== null) {
    dialogAddress.quId = dialogAddress.villageList[item].code
    dialogAddress.qu = dialogAddress.villageList[item].name
  }
}

const address = {
  provinceId: '',
  cityId: '',
  villageId: '',
  provinceList: [],
  cityList: [],
  villageList: [],
  shengId: '',
  sheng: '',
  shiId: '',
  shi: '',
  quId: '',
  qu: ''
}
const dialogAddress = {
  provinceId: '',
  cityId: '',
  villageId: '',
  provinceList: [],
  cityList: [],
  villageList: [],
  shengId: '',
  sheng: '',
  shiId: '',
  shi: '',
  quId: '',
  qu: ''
}
export { address, dialogAddress }
